// pages/song/song.js
import request from '../../utils/request'
import PubSub from 'pubsub-js'
import moment from 'moment'
var appInstance = getApp()
Page({
  
  /**
   * 页面的初始数据
   */
  data: {
    musicId:'',
    isPlay:false,
    music:{},
    currentTime:'00:00',
    durationTime:'00:00',
    musicLink:'',
    currentWidth:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //获取musicId
    const musicId = options.songId
    this.setData({
      musicId
    })
    this.getMusicDetail(this.data.musicId)
    this.bgAudioManager = wx.getBackgroundAudioManager()
    if(appInstance.globalData.musicId.toString() === musicId.toString() && appInstance.globalData.playStatus){
      this.setData({
        isPlay:true
      })
    }
    this.bgAudioManager.onPlay(()=>{
      appInstance.globalData.musicId = musicId
      this.changeMusicState(true)
    })
    this.bgAudioManager.onPause(()=>{
      this.changeMusicState(false)
    })
    this.bgAudioManager.onStop(()=>{
      this.changeMusicState(false)
    })
    this.bgAudioManager.onTimeUpdate(()=>{
      const currentTime = moment(this.bgAudioManager.currentTime * 1000).format('mm:ss')
      const currentWidth = 450 * this.bgAudioManager.currentTime / this.bgAudioManager.duration
      this.setData({
        currentTime,
        currentWidth
      })
    })
    this.bgAudioManager.onEnded(()=>{
      this.setData({
        currentTime: '00:00',
        currentWidth:0
      })
      this.handlerSwitch({
        currentTarget:{
          id:'next'
        }
      })
    })
  },
  // 播放/暂停状态
  changeMusicState(isPlay){
    this.setData({
      isPlay
    })
    appInstance.globalData.playStatus = isPlay
  },
  // 获取音乐详情数据
  async getMusicDetail(ids){
    const song = await request('/song/detail',{ids})
    const durationTime = moment(song.songs[0].dt).format('mm:ss')
    this.setData({
      music:song.songs[0],
      durationTime
    })
    wx.setNavigationBarTitle({
      title: this.data.music.name
    })
  },
  // 点击播放
  handlerPlay(){
    const isPlay = !this.data.isPlay
    this.setData({
      isPlay
    })
    const { musicId,musicLink } = this.data
    this.musicControl(isPlay,musicId,musicLink)
  },
  // 控制播放
  async musicControl (isPlay,musicId,musicLink){
    if(!isPlay){
      this.bgAudioManager.pause()
      return
    }
    if(!musicLink){
      const musicLinkData = await request('/song/url',{ id:musicId })
      musicLink = musicLinkData.data[0].url
      this.setData({
        musicLink
      })
    }
    if(!musicLink){
      wx.showToast({
        title: '该歌曲需要开通会员',
        icon:'none'
      })
      return
    }
    this.bgAudioManager.src = musicLink
    this.bgAudioManager.title = this.data.music.name
  },
  // 上一首&下一首
  handlerSwitch(event){
    PubSub.subscribe('musicId',(msg,musicId)=>{
      this.setData({
        musicId
      })
      this.getMusicDetail(musicId)
      this.musicControl(true,musicId)
      PubSub.unsubscribe('musicId')
    })
    const target = event.currentTarget.id
    PubSub.publish('switchType',target)
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})